#include <ctype.h>

/* Convert \t \xDDD \DDD etc to characters. 'src' and 'dest' can be the same. */

/*
    Contact info:
    bhepple@freeshell.org
    http://bhepple.freeshell.org

    Copyright (C) 1999-2008 Bob Hepple

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; see the file COPYING.  If not, write to
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.

*/

void undo_escapes(dest, src)
	char 	*dest, *src;
{
	int 	i, count;

	while (*src) {
		if (*src != '\\')
			*dest++ = *src++;
		else if (isascii(*++src))
			switch (tolower(*src)) {
			case 'a' : *dest++ = 0x07;	src++; break; /* bell 	 	 */
			case 'b' : *dest++ = 0x08;	src++; break; /* backspace 	 */
			case 'f' : *dest++ = 0x0C;	src++; break; /* formfeed 	 */
			case 'n' : *dest++ = 0x0A;	src++; break; /* newline	 */
			case 'r' : *dest++ = 0x0D;	src++; break; /* carriage return */
			case 't' : *dest++ = 0x09;	src++; break; /* tab		 */
			case 'v' : *dest++ = 0x0B;	src++; break; /* vertical tab	 */
			case '\\': *dest++ = 0x5C;	src++; break;
			case '\'': *dest++ = 0x2C;	src++; break;
			case '"' : *dest++ = 0x22;	src++; break;
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
				i = 0;
				count = 0;
				while (*src && isascii(*src) && isdigit(*src) && 
				       (*src < '8') && 
				       (count < 3)) {
					i *= 8;
					i += (*src++ - '0');
					count++;
				}
				*dest++ = i;
				break;
			case 'x':
				src++;
				i = 0;
				count = 0;
				while (*src && isascii(*src) && isxdigit(*src) && (count < 2)) {
					i *= 16;
					if (isdigit(*src))
						i += (*src - '0');
					else
						i += (tolower(*src) - 'a');
					src++;
					count++;
				}
				*dest++ = i;
				break;
			default:
				*dest++ = *src++;
				break;
		}
	}
	*dest = 0;
}


/* For emacs:
 * Local Variables:
 * tab-width: 8
 * End:
 */
